{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu=70 is True.\n"
     ]
    }
   ],
   "source": [
    "#例7-8\n",
    "from utility import ztest\n",
    "import numpy as np\n",
    "x=np.array([65, 72, 89, 56, 79, 63, 92, 48, 75, 81])\n",
    "xmean=x.mean()\n",
    "n=x.size\n",
    "s0=10\n",
    "mu0=70\n",
    "alpha=0.05\n",
    "z=(xmean-mu0)/(s0/np.sqrt(n))\n",
    "pvalue=ztest(z)\n",
    "print('mu=%d is %s.'%(mu0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu=0.618 is True.\n"
     ]
    }
   ],
   "source": [
    "#练习7-8\n",
    "from utility import ztest\n",
    "import numpy as np\n",
    "x=np.array([0.693, 0.749, 0.654, 0.670, 0.662,\n",
    "            0.672, 0.615, 0.606, 0.690, 0.628,\n",
    "            0.668, 0.611, 0.606, 0.609, 0.601,\n",
    "            0.553, 0.570, 0.844, 0.576, 0.933])\n",
    "xmean=x.mean()\n",
    "s0=0.01\n",
    "mu0=0.618\n",
    "n=x.size\n",
    "alpha=0.05\n",
    "z=(xmean-mu0)/np.sqrt(s0/n)\n",
    "pvalue=ztest(z)\n",
    "print('mu=%.3f is %s.'%(mu0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu<=-0.545 is False.\n"
     ]
    }
   ],
   "source": [
    "#例7-9\n",
    "from utility import ztest\n",
    "xmean=-0.535\n",
    "s0=0.008\n",
    "mu0=-0.545\n",
    "n=5\n",
    "alpha=0.05\n",
    "z=(xmean-mu0)/(s0/np.sqrt(n))\n",
    "pvalue=ztest(z, alternative='greater')\n",
    "print('mu<=%.3f is %s.'%(mu0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu>=1000 is False.\n"
     ]
    }
   ],
   "source": [
    "#练习7-9\n",
    "from utility import ztest\n",
    "import numpy as np\n",
    "xmean=950\n",
    "sigma=100\n",
    "mu0=1000\n",
    "n=25\n",
    "alpha=0.05\n",
    "z=(xmean-mu0)/(s0/np.sqrt(n))\n",
    "pvalue=ztest(z, alternative='less')\n",
    "print('mu>=%d is %s.'%(mu0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu=3.25 is True.\n"
     ]
    }
   ],
   "source": [
    "#例7-10\n",
    "from utility import ttest\n",
    "import numpy as np\n",
    "x=np.array([3.25, 3.27, 3.24, 3.26, 3.24])\n",
    "xmean=x.mean()\n",
    "s=x.std(ddof=1)\n",
    "n=x.size\n",
    "mu0=3.25\n",
    "alpha=0.01\n",
    "T=(xmean-mu0)/(s/np.sqrt(n))\n",
    "pvalue=ttest(T, n-1)\n",
    "print('mu=%.2f is %s.'%(mu0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu=38.4 is True.\n"
     ]
    }
   ],
   "source": [
    "#练习7-10\n",
    "from utility import ttest\n",
    "xmean=40.5\n",
    "s=7.5\n",
    "n=15\n",
    "mu0=38.4\n",
    "alpha=0.05\n",
    "T=(xmean-mu0)/(s/np.sqrt(n))\n",
    "pvalue=ttest(T, n-1)\n",
    "print('mu=%.1f is %s.'%(mu0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu>=225 is True.\n"
     ]
    }
   ],
   "source": [
    "#例7-11\n",
    "from utility import ttest\n",
    "import numpy as np\n",
    "x=np.array([159, 280, 101, 212, 224, 379, 179, 264,\n",
    "           222, 362, 168, 250, 149, 260, 485, 170])\n",
    "xmean=x.mean()\n",
    "s=x.std(ddof=1)\n",
    "n=16\n",
    "mu0=225\n",
    "alpha=0.05\n",
    "T=(xmean-mu0)/(s/np.sqrt(n))\n",
    "pvalue=ttest(T, n-1, alternative='less')\n",
    "print('mu>=%d is %s.'%(mu0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu>10 is True.\n"
     ]
    }
   ],
   "source": [
    "#练习7-11\n",
    "from utility import ttest\n",
    "import numpy as np\n",
    "x=np.array([9.8, 10.4, 10.6, 9.6, 9.7,\n",
    "            9.9, 10.9, 11.1, 9.6, 10.2,\n",
    "            10.3, 9.6, 9.9, 11.2, 10.6,\n",
    "            9.8, 10.5, 10.1, 10.5, 9.7])\n",
    "xmean=x.mean()\n",
    "s=x.std(ddof=1)\n",
    "n=x.size\n",
    "mu0=10\n",
    "alpha=0.05\n",
    "T=(xmean-mu0)/(s/np.sqrt(n))\n",
    "pvalue=ttest(T, n-1, alternative='less')\n",
    "print('mu>%d is %s.'%(mu0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma^2=1.6^2 is True.\n",
      "mu=52.8 is True\n"
     ]
    }
   ],
   "source": [
    "#例7-12\n",
    "from utility import ztest, chi2test\n",
    "import numpy as np\n",
    "x=np.array([51.9, 53.0, 52.7, 54.1, 53.2, 52.3, 52.5, 51.1, 54.7])\n",
    "n=x.size\n",
    "xmean=x.mean()\n",
    "s2=x.var(ddof=1)\n",
    "sigma0=1.6\n",
    "mu0=52.8\n",
    "alpha=0.1\n",
    "X=(n-1)*s2/sigma0**2\n",
    "pvalue=chi2test(X, n-1)\n",
    "print('sigma^2=%.1f^2 is %s.'%(sigma0, pvalue>=alpha))\n",
    "Z=(xmean-mu0)/(sigma0/np.sqrt(n))\n",
    "pvalue=ztest(Z)\n",
    "print('mu=%.1f is %s'%(mu0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma^2=5000.0^2 is False.\n"
     ]
    }
   ],
   "source": [
    "#练习7-12\n",
    "from utility import ztest, chi2test\n",
    "import numpy as np\n",
    "n=26\n",
    "s2=9200\n",
    "sigma02=5000\n",
    "alpha=0.02\n",
    "X=(n-1)*s2/sigma02\n",
    "pvalue=chi2test(X, n-1)\n",
    "print('sigma^2=%.1f^2 is %s.'%(sigma02, pvalue>=alpha))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma^2>0.048^2 is True.\n"
     ]
    }
   ],
   "source": [
    "#例7-13\n",
    "from utility import chi2test\n",
    "import numpy as np\n",
    "x=np.array([1.32, 1.55, 1.36, 1.40, 1.44])\n",
    "n=x.size\n",
    "s2=x.var(ddof=1)\n",
    "sigma0=0.048\n",
    "alpha=0.05\n",
    "X=(n-1)*s2/sigma0**2\n",
    "pvalue=chi2test(X, n-1, alternative='less')\n",
    "print('sigma^2>%.3f^2 is %s.'%(sigma0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma^2<=0.15^2 is True.\n"
     ]
    }
   ],
   "source": [
    "#练习7-13\n",
    "from utility import chi2test\n",
    "n=10\n",
    "s2=0.028\n",
    "sigma0=0.15\n",
    "alpha=0.05\n",
    "X=(n-1)*s2/sigma0**2\n",
    "pvalue=chi2test(X, n-1, alternative='greater')\n",
    "print('sigma^2<=%.2f^2 is %s.'%(sigma0, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu1-mu2=0 is False.\n"
     ]
    }
   ],
   "source": [
    "#例7-20\n",
    "import numpy as np\n",
    "from utility import ztest\n",
    "xmean=1295\n",
    "ymean=1230\n",
    "xsigma2=84**2\n",
    "ysigma2=96**2\n",
    "delta=0\n",
    "n1=60\n",
    "n2=60\n",
    "alpha=0.05\n",
    "Z=(xmean-ymean)/np.sqrt(xsigma2/n1+ysigma2/n2)\n",
    "pvalue=ztest(Z)\n",
    "print('mu1-mu2=0 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu1-mu2=0 is True.\n"
     ]
    }
   ],
   "source": [
    "#练习7-20\n",
    "from utility import ztest\n",
    "import numpy as np\n",
    "x=np.array([65, 72, 89, 56, 79, 63, 92, 48, 75, 81])\n",
    "y=np.array([78, 69, 65, 61, 54, 87, 51, 67])\n",
    "xmean=x.mean()\n",
    "ymean=y.mean()\n",
    "xsigma2=10**2\n",
    "ysigma2=9.5**2\n",
    "n1=x.size\n",
    "n2=y.size\n",
    "alpha=0.05\n",
    "Z=(xmean-ymean)/np.sqrt(xsigma2/n1+ysigma2/n2)\n",
    "pvalue=ztest(Z)\n",
    "print('mu1-mu2=0 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu1-mu2>=120 is False.\n"
     ]
    }
   ],
   "source": [
    "#例7-21\n",
    "import numpy as np\n",
    "from utility import ztest\n",
    "xmean=867\n",
    "ymean=778\n",
    "xsigma2=62.8**2\n",
    "ysigma2=56.1**2\n",
    "delta=120\n",
    "n1=50\n",
    "n2=50\n",
    "alpha=0.05\n",
    "Z=(xmean-ymean-delta)/np.sqrt(xsigma2/n1+ysigma2/n2)\n",
    "pvalue=ztest(Z,alternative='less')\n",
    "print('mu1-mu2>=%d is %s.'%(delta, pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu1-mu<=0 is False\n"
     ]
    }
   ],
   "source": [
    "#练习7-21\n",
    "import numpy as np\n",
    "from utility import ztest\n",
    "x=np.array([79.98,80.04,80.02,80.04,80.03,80.03,80.04,\n",
    "            79.97,80.05,80.03,80.02,80.00,80.02])\n",
    "y=np.array([80.02,79.94,79.98,79.97,79.97,80.03,79.95,79.97])\n",
    "xmean=x.mean()\n",
    "ymean=y.mean()\n",
    "s12=0.024**2\n",
    "s22=0.031**2\n",
    "n1=x.size\n",
    "n2=y.size\n",
    "alpha=0.05\n",
    "Z=(xmean-ymean)/np.sqrt(s12/n1+s22/n2)\n",
    "pvalue=ztest(Z, alternative='greater')\n",
    "print('mu1-mu<=0 is %s'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu1-mu2=0 is False.\n"
     ]
    }
   ],
   "source": [
    "#例7-22\n",
    "from utility import ttest\n",
    "import numpy as np\n",
    "x=np.array([65, 60, 62, 57, 58, 63, 60, 57, 60, 58]) \n",
    "y=np.array([59, 56, 56, 58, 57, 57, 55, 60, 57, 55])\n",
    "xmean=x.mean()\n",
    "ymean=y.mean()\n",
    "s12=x.var(ddof=1)\n",
    "s22=y.var(ddof=1)\n",
    "n1=x.size\n",
    "n2=y.size\n",
    "alpha=0.05\n",
    "sw=np.sqrt(((n1-1)*s12+(n2-1)*s22)/(n1+n2-2))\n",
    "T=(xmean-ymean)/(sw*np.sqrt(1/n1+1/n2))\n",
    "pvalue=ttest(T, n1+n2-2)\n",
    "print('mu1-mu2=0 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu1-mu2=0 is False.\n"
     ]
    }
   ],
   "source": [
    "#练习7-22\n",
    "from utility import ttest\n",
    "import numpy as np\n",
    "x=np.array([0.225,0.262,0.217,0.240,0.230,0.229,0.235,0.217]) \n",
    "y=np.array([0.209,0.205,0.198,0.210,0.202,0.207,0.224,0.223,0.220,0.201])\n",
    "xmean=x.mean()\n",
    "ymean=y.mean()\n",
    "s12=x.var(ddof=1)\n",
    "s22=y.var(ddof=1)\n",
    "n1=x.size\n",
    "n2=y.size\n",
    "alpha=0.05\n",
    "sw=np.sqrt(((n1-1)*s12+(n2-1)*s22)/(n1+n2-2))\n",
    "T=(xmean-ymean)/(sw*np.sqrt(1/n1+1/n2))\n",
    "pvalue=ttest(T, n1+n2-2)\n",
    "print('mu1-mu2=0 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu1<=mu2 is False.\n"
     ]
    }
   ],
   "source": [
    "#例7-23\n",
    "from utility import ttest\n",
    "import numpy as np\n",
    "x=np.array([65, 68, 72, 75, 82, 85, 87, 91, 95]) \n",
    "y=np.array([50, 59, 71, 80])\n",
    "xmean=x.mean()\n",
    "ymean=y.mean()\n",
    "s12=x.var(ddof=1)\n",
    "s22=y.var(ddof=1)\n",
    "n1=x.size\n",
    "n2=y.size\n",
    "alpha=0.05\n",
    "sw=np.sqrt(((n1-1)*s12+(n2-1)*s22)/(n1+n2-2))\n",
    "T=(xmean-ymean)/(sw*np.sqrt(1/n1+1/n2))\n",
    "pvalue=ttest(T, n1+n2-2, alternative='greater')\n",
    "print('mu1<=mu2 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu1>=mu2 is True.\n"
     ]
    }
   ],
   "source": [
    "#练习7-23\n",
    "from utility import ttest\n",
    "import numpy as np\n",
    "x=np.array([80, 90, 79, 67, 85]) \n",
    "y=np.array([83, 65, 63, 64, 67, 56, 57, 58, 60,\n",
    "            78, 67, 71, 90, 73, 71, 77, 72, 68,\n",
    "            63, 53, 70, 88, 74, 66, 60, 64])\n",
    "xmean=x.mean()\n",
    "ymean=y.mean()\n",
    "s12=x.var(ddof=1)\n",
    "s22=y.var(ddof=1)\n",
    "n1=x.size\n",
    "n2=y.size\n",
    "alpha=0.05\n",
    "sw=np.sqrt(((n1-1)*s12+(n2-1)*s22)/(n1+n2-2))\n",
    "T=(xmean-ymean)/(sw*np.sqrt(1/n1+1/n2))\n",
    "pvalue=ttest(T, n1+n2-2, alternative='less')\n",
    "print('mu1>=mu2 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma1^2=sigma2^2 is True.\n",
      "mu1=mu2 is True.\n"
     ]
    }
   ],
   "source": [
    "#例7-24\n",
    "from utility import ttest, ftest\n",
    "import numpy as np\n",
    "x=np.array([5.5, 4.6, 4.4, 3.4, 1.9, 1.6, 1.1, 0.8, 0.1, -0.1])\n",
    "y=np.array([3.7, 3.4, 2.0, 2.0, 0.8, 0.7, 0, -0.1, -.2, -1.6])\n",
    "xmean=x.mean()\n",
    "ymean=y.mean()\n",
    "s12=x.var(ddof=1)\n",
    "s22=y.var(ddof=1)\n",
    "n1=x.size\n",
    "n2=y.size\n",
    "alpha=0.05\n",
    "F=s12/s22\n",
    "pvalue=ftest(F, n1-1, n2-1)\n",
    "print('sigma1^2=sigma2^2 is %s.'%(pvalue>=alpha))\n",
    "sw=np.sqrt(((n1-1)*s12+(n2-1)*s22)/(n1+n2-2))\n",
    "T=(xmean-ymean)/(sw*np.sqrt(1/n1+1/n2))\n",
    "pvalue=ttest(T, n1+n2-2)\n",
    "print('mu1=mu2 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma1^2=sigma2^2 is True.\n",
      "mu1=mu2 is True.\n"
     ]
    }
   ],
   "source": [
    "#练习7-24\n",
    "import numpy as np\n",
    "from utility import ttest, ftest\n",
    "x=np.array([49, 48, 47, 53, 51, 43, 39, 57, 56, 46, 42, 44, 55, 44, 40])\n",
    "y=np.array([46, 40, 47, 51, 43, 36, 43, 38, 48, 54, 48, 34])\n",
    "xmean=x.mean()\n",
    "ymean=y.mean()\n",
    "s12=x.var(ddof=1)\n",
    "s22=y.var(ddof=1)\n",
    "n1=x.size\n",
    "n2=y.size\n",
    "alpha=0.05\n",
    "F=s12/s22\n",
    "pvalue=ftest(F, n1-1, n2-1)\n",
    "print('sigma1^2=sigma2^2 is %s.'%(pvalue>=alpha))\n",
    "sw=np.sqrt(((n1-1)*s12+(n2-1)*s22)/(n1+n2-2))\n",
    "T=(xmean-ymean)/(sw*np.sqrt(1/n1+1/n2))\n",
    "pvalue=ttest(T, n1+n2-2)\n",
    "print('mu1=mu2 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma1^2>sigma2^2 is True.\n"
     ]
    }
   ],
   "source": [
    "#例7-25\n",
    "import numpy as np\n",
    "from utility import ftest\n",
    "x=np.array([93.3, 92.1, 94.7, 90.1, 95.6, 90.0, 94.7])\n",
    "y=np.array([95.6, 94.9, 96.2, 95.8, 95.1, 96.3])\n",
    "s12=x.var(ddof=1)\n",
    "s22=y.var(ddof=1)\n",
    "n1=x.size\n",
    "n2=y.size\n",
    "alpha=0.05\n",
    "F=s12/s22\n",
    "pvalue=ftest(F, n1-1, n2-1, alternative='less')\n",
    "print('sigma1^2>sigma2^2 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma1^2<=sigma2^2 is True.\n"
     ]
    }
   ],
   "source": [
    "#练习7-25\n",
    "from utility import ftest\n",
    "s12=15.46\n",
    "s22=9.66\n",
    "n1=60\n",
    "n2=40\n",
    "alpha=0.05\n",
    "F=s12/s22\n",
    "pvalue=ftest(F, n1-1, n2-1, alternative='greater')\n",
    "print('sigma1^2<=sigma2^2 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu=0 is True.\n"
     ]
    }
   ],
   "source": [
    "#例7-30\n",
    "import numpy as np\n",
    "from scipy.stats import ttest_1samp\n",
    "x=np.array([23, 31, 25, 18, 19, 25, 28,\n",
    "            18, 25, 28, 22, 14, 34, 36])\n",
    "y=np.array([22, 31, 29, 24, 28, 31, 27,\n",
    "            15, 23, 27, 26, 19, 30, 28])\n",
    "n=x.size\n",
    "alpha=0.05\n",
    "d=x-y\n",
    "_,pvalue=ttest_1samp(d, 0)\n",
    "print('mu=0 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu>=0 is False\n"
     ]
    }
   ],
   "source": [
    "#练习7-30\n",
    "import numpy as np\n",
    "from scipy.stats import ttest_1samp\n",
    "x=np.array([0.30, 0.23, 0.41, 0.53, 0.24, 0.36, 0.38, 0.51])\n",
    "y=np.array([0.43, 0.32, 0.58, 0.46, 0.27, 0.41, 0.38, 0.61])\n",
    "n=x.size\n",
    "alpha=0.05\n",
    "d=x-y\n",
    "v,pvalue=ttest_1samp(d, 0, alternative='less')\n",
    "print('mu>=0 is %s'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H0 is True.\n"
     ]
    }
   ],
   "source": [
    "#例7-31\n",
    "from scipy.stats import poisson, chisquare\n",
    "import numpy as np\n",
    "n=100\n",
    "alpha=0.05\n",
    "f=np.array([1,5,16,17,26,11,9,9,2,1,2,1,0])\n",
    "k=f.size\n",
    "r=1\n",
    "x_bar=(np.arange(k)*f).sum()/n\n",
    "p=[poisson.pmf(i,x_bar) for i in range(k-1)]\n",
    "p.append(1-sum(p))\n",
    "p=np.array(p)\n",
    "x, pv=chisquare(f, p*n, r)\n",
    "print('H0 is %s.'%(pv>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H0 is False.\n"
     ]
    }
   ],
   "source": [
    "#练习7-31\n",
    "from scipy.stats import poisson, chisquare\n",
    "import numpy as np\n",
    "n=100\n",
    "alpha=0.05\n",
    "f=np.array([22,37,20,13,6,2,0])\n",
    "k=f.size\n",
    "r=0\n",
    "p=[poisson.pmf(i,1) for i in range(k-1)]\n",
    "p.append(1-sum(p))\n",
    "p=np.array(p)\n",
    "x, pv=chisquare(f,p*n)\n",
    "print('H0 is %s.'%(pv>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H0 is False.\n"
     ]
    }
   ],
   "source": [
    "#例7-32\n",
    "from scipy.stats import chisquare\n",
    "import numpy as np\n",
    "from utility import margDist\n",
    "alpha=0.05\n",
    "f=np.array([[58, 38, 32],\n",
    "            [28, 44, 45],\n",
    "            [23, 18, 14]])\n",
    "(u, v)=f.shape\n",
    "n=f.sum()\n",
    "fi, fj=margDist(f)\n",
    "fij=fi*fj/n\n",
    "chiq, pvalue=chisquare(f.reshape(u*v,),\n",
    "                       fij.reshape(u*v,),\n",
    "                       ddof=u+v-2)\n",
    "print('H0 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H0 is False.\n"
     ]
    }
   ],
   "source": [
    "#练习7-32\n",
    "from scipy.stats import chisquare\n",
    "import numpy as np\n",
    "from utility import margDist\n",
    "alpha=0.05\n",
    "f=np.array([[442, 514],\n",
    "            [38, 6]])\n",
    "(u, v)=f.shape\n",
    "n=f.sum()\n",
    "fi, fj=margDist(f)\n",
    "fij=fi*fj/n\n",
    "chiq, pvalue=chisquare(f.reshape(u*v,),\n",
    "                       fij.reshape(u*v,),\n",
    "                       ddof=u+v-2)\n",
    "print('H0 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H0 is True.\n"
     ]
    }
   ],
   "source": [
    "#例7-33\n",
    "from scipy.stats import chisquare\n",
    "import numpy as np\n",
    "from utility import margDist\n",
    "alpha=0.05\n",
    "f=np.array([[1021, 1073, 1015],\n",
    "            [229, 256, 265],\n",
    "            [124, 166, 139],\n",
    "            [34, 44, 25],\n",
    "            [9, 11, 4]])\n",
    "(u, v)=f.shape\n",
    "n=f.sum()\n",
    "fi, fj=margDist(f)\n",
    "fij=fi*fj/n\n",
    "chiq, pvalue=chisquare(f.reshape(u*v,),\n",
    "                       fij.reshape(u*v,),\n",
    "                       ddof=u+v-2)\n",
    "print('H0 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H0 is False.\n"
     ]
    }
   ],
   "source": [
    "#练习7-33\n",
    "from scipy.stats import chisquare\n",
    "import numpy as np\n",
    "from utility import margDist\n",
    "alpha=0.05\n",
    "f=np.array([[252, 224],\n",
    "            [145, 136],\n",
    "            [103, 140]])\n",
    "(u, v)=f.shape\n",
    "n=f.sum()\n",
    "fi, fj=margDist(f)\n",
    "fij=fi*fj/n\n",
    "chiq, pvalue=chisquare(f.reshape(u*v,),\n",
    "                       fij.reshape(u*v,),\n",
    "                       ddof=u+v-2)\n",
    "print('H0 is %s.'%(pvalue>=alpha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.08333333333333331"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from utility import base\n",
    "class mydist(base):\n",
    "    def _pdf(self, x):\n",
    "        if type(x)==float:\n",
    "            x=np.array([x])\n",
    "        y=np.zeros(x.size)\n",
    "        d=np.where((x>0)&(x<1))\n",
    "        y[d]=1\n",
    "        return y\n",
    "dist=mydist()\n",
    "Ex=dist.expect()\n",
    "Dx=dist.var()\n",
    "Dx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
